package com.system.main.controller;

import com.system.main.model.*;
import com.system.main.service.UserService;
import com.system.main.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*", maxAge=3600)
@Slf4j
@RestController
public class AdminController {
    @Autowired
    private UserService userService;

    @PostMapping("/getAllUser")
    @ResponseBody
    public Response getAllUser(@RequestBody Token token) {
        User user = new User();
        user = JwtUtil.getUserInfoByJwt(token.getToken());
        Response response = new Response();
        if(user!=null){
            if(user.getType()==0){
                response = successResponse(response);
                response.setData(new Info("allUser", userService.getAllUsers()));
            }else {
                response = failResponse(response,"您不是管理员身份。");
            }
        }else{
            response = failResponse(response,"登录信息有误");
        }
        return response;
    }

    private Response successResponse(Response response) {
        response.setCode(0);
        response.setMsg("success");
        return response;
    }

    private Response failResponse(Response response, String msg) {
        response.setCode(1);
        response.setMsg(msg);
        return response;
    }
}
